import { loadMdicMap } from '@/services/BaseLayout';
import { getBaseUrl } from '@/services/urlconst';
import {
  getCompanyID,
  getPosIDs,
  getTenantID,
  getUserDepID,
  getUserID,
  requestFunComponFiex,
} from '@/utils';
import { message } from 'antd';
import { ISO_8601 } from 'moment';
import { useMemo } from 'react';
import { useRequest } from 'umi';

let baseUrl = getBaseUrl();
// let baseUrl = 'http://192.168.3.6:9090'; //少勇本地

/**获取数据字典 */
const getLoadMapUpdate = (curRes: Record<string, any>) => {
  //获取数据字典loadMdicMap
  const loadMdicMapList = useRequest(() => {
    return loadMdicMap([`${getTenantID()}1201,${getTenantID()}1202`]);
  });

  // 数据字典数据
  let mapDataList = useMemo(() => {
    if (loadMdicMapList?.data?.msgFlag) {
      return loadMdicMapList?.data?.obj;
    } else {
      return {};
    }
  }, [loadMdicMapList?.data]);

  return {
    mapDataList,
    mapLoading: loadMdicMapList?.loading,
  };
};

/**
 * 获取组织机构接口
 */
const getFlowOrgTreeByOrgIDService = () => {
  const getFlowOrgTreeByOrgID = useRequest(
    (payload: any) => {
      let reqUrl = `${getBaseUrl()}/commonController/getFlowOrgTreeByOrgID/${
        payload?.params
      }/${payload?.flowType}/${payload?.orgID}`;
      let methodType = 'GET';
      return requestFunComponFiex(methodType, reqUrl, {});
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (!(res && res?.msgFlag)) {
          message.error(res?.msg);
        }
      },
    },
  );

  // 处理数据
  let getFlowOrgList = useMemo(() => {
    if (getFlowOrgTreeByOrgID?.data?.msgFlag) {
      return getFlowOrgTreeByOrgID?.data?.obj;
    } else {
      return [];
    }
  }, [getFlowOrgTreeByOrgID?.data]);

  return {
    getFlowOrgTreeRun: getFlowOrgTreeByOrgID?.run,
    getFlowOrgTreeLoading: getFlowOrgTreeByOrgID?.loading,
    getFlowOrgList: getFlowOrgList,
  };
};

/**
 * 获取相关版本
 * @param getVersionDataCallBack
 * @returns
 */
const getVersionDataSercvice = (getVersionDataCallBack: (res: any) => void) => {
  const getVersionData = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/versionController/dataGrid`;
      let bodyParams = {
        ...params,
        // page:1,
        // rows:10,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: getVersionDataCallBack,
    },
  );
  return {
    getVersionDataRun: getVersionData?.run,
    getVersionDataLoading: getVersionData?.loading,
  };
};

/**
 * 获取系统版本详情
 * @param getVersionDetailCallBack  获取系统版本详情回调
 * @returns
 */
const getVersionDetailService = (
  getVersionDetailCallBack: (res: any) => void,
) => {
  const getVersionDetail = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/versionController/getVersionByID/${payload.id}`;
      let methodType = 'GET';
      return requestFunComponFiex(methodType, reqUrl, {});
    },
    {
      manual: true,
      onSuccess: getVersionDetailCallBack,
    },
  );
  return {
    getVersionDetailRun: getVersionDetail?.run,
    getVersionDetailLoading: getVersionDetail?.loading,
    getVersionDetailData: getVersionDetail?.data?.obj,
  };
};

/**
 * 获取当前实时的版本信息
 */
const getRealTimeVersionDataService = (
  getRealTimeVersionDataCallBack: (res: any) => void,
) => {
  const getVersionRealData = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/versionController/getVersionList`;
      let bodyParams = {
        ...params,
        versionState: 1,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: getRealTimeVersionDataCallBack,
    },
  );

  return {
    getVersionRealDataRun: getVersionRealData?.run,
    getVersionRealDataLoading: getVersionRealData?.loading,
    getVersionRealData: getVersionRealData?.data,
  };
};

/**
 * 新增版本
 * @param addVersionCallBack 新增接口调用后的回调
 * @returns
 */
const addVersionService = (
  updateVersionCallback: (res: any, callType: boolean) => void,
) => {
  const addVersion = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/versionController/add`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        updateVersionCallback(res, true);
      },
    },
  );

  const data = useMemo(() => {
    return addVersion?.data?.obj;
  }, [addVersion?.data]);

  return {
    addVersionRun: addVersion?.run,
    addVersionLoading: addVersion?.loading,
    addVersionData: data,
  };
};

/**
 * 编辑版本
 * @param editVersionCallBack 新增接口调用后的回调
 * @returns
 */
const editVersionService = (
  updateVersionCallback: (res: any, callType: boolean) => void,
) => {
  const editVersion = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/versionController/edit`;
      let methodType = 'PUT';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        updateVersionCallback(res, false);
      },
    },
  );
  return {
    editVersionRun: editVersion?.run,
    editVersionLoading: editVersion?.loading,
  };
};

/**
 * 提交申请
 * @param checkIssueCallBack 接口调用后的回调
 * @returns
 */
const applyService = (applyCallBack: (res: any) => void) => {
  const applyIssue = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        applyerID: getUserID(),
        applyPositionID: getPosIDs(),
        applyDepartmentID: getUserDepID(),
        applyCompanyID: getCompanyID(),
        // checkerID: getUserID(),
        // checkPositionID: getPosIDs(),
        // checkDepartmentID: getUserDepID(),
        // checkCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/versionController/apply`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: applyCallBack,
    },
  );

  return {
    applyIssueRun: applyIssue?.run,
    applyIssueLoading: applyIssue?.loading,
    applyIssueData: applyIssue?.data?.obj,
  };
};

/**
 * 审核
 * @param checkIssueCallBack 接口调用后的回调
 * @returns
 */
const checkIssueService = (checkIssueCallBack: (res: any) => void) => {
  const checkIssue = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        checkerID: getUserID(),
        checkPositionID: getPosIDs(),
        checkDepartmentID: getUserDepID(),
        checkCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/versionController/checkIssue`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: checkIssueCallBack,
    },
  );
  return {
    checkIssueRun: checkIssue?.run,
    checkIssueLoading: checkIssue?.loading,
  };
};

/**
 * 直接签发
 * @param directlyIssueCallBack 接口调用后的回调
 * @returns
 */
const directlyIssueService = (directlyIssueCallBack: (res: any) => void) => {
  const Issue = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        applyerID: getUserID(),
        applyPositionID: getPosIDs(),
        applyDepartmentID: getUserDepID(),
        applyCompanyID: getCompanyID(),
        checkerID: getUserID(),
        checkPositionID: getPosIDs(),
        checkDepartmentID: getUserDepID(),
        checkCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/versionController/issue`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: directlyIssueCallBack,
    },
  );
  return {
    directlyIssueRun: Issue?.run,
    directlyIssueLoading: Issue?.loading,
    directlyIssueData: Issue?.data,
  };
};

export {
  getLoadMapUpdate, //数据字典
  getFlowOrgTreeByOrgIDService, //组织机构
  getVersionDataSercvice, //相关版本
  getVersionDetailService, //系统版本详情
  getRealTimeVersionDataService, //当前实时的版本信息
  applyService, //提交申请
  checkIssueService, //审核
  directlyIssueService, //直接签发
  addVersionService, //新增
  editVersionService, //编辑
};
